package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RequestMapping("/dish")
@RestController

public class DishDaoController {
    @Autowired(required = false)
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        dishDto.setCreateUser(empId);
        //保存菜品与口味
        dishService.saveWithFlavor(dishDto);
        return R.success("保存菜品成功");
    }

    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        Page<DishDto> page1 = dishService.page(page, pageSize, name);
        return R.success(page1);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

}


